var log_div = null;

function get_url_value(id) {
	var url_prefix = dao.getUrlPrefix();
	if (url_prefix == null) url_prefix = "";
	var url = document.getElementById(id).value;
	if (url == '') return null;
	if (url_prefix.length > 0 && url.length > url_prefix.length && url.substring(0, url_prefix.length) == url_prefix) {
		url = url.substring(url_prefix.length);
		return url;
	}
}

function get_sel_value(id) {
	var sel = document.getElementById(id);
	if (sel == null) return null;
	return sel.options[sel.selectedIndex].value;
}

function get_content(url, callback) {
	url = append_prefix(url);
	var spider = nest.callSpider();
	spider.eat(url, callback, 200, false);
}

function append_prefix(url) {
	var url_perfix = dao.getUrlPrefix();
	if (url_prefix == null) return url;
	if (url_prefix.length > 0 && url.length > url_prefix.length && url.substring(0, url_prefix.length) == url_prefix)
		return url;
	else
		return url_prefix + url;
}

function remove_prefix(url) {
	var url_perfix = dao.getUrlPrefix();
	if (url_prefix == null) return url;
	if (url_prefix.length > 0 && url.length > url_prefix.length && url.substring(0, url_prefix.length) == url_prefix)
		return url.substring(url_prefix.length);
	else
		return url;
}

var __get_titles_callback = null;

function get_titles(ary, callback) {
	if (callback == null) {
		error('No callback for get titles');
	}
	__get_titles_callback = callback;
	for (var i in ary) {
		get_content(ary[i], get_titles_callback);
	}
}

function get_titles_callback(url, hasError, content) {
	url = remove_prefix(url);
	sis_set_content(url_prefix, content);
	var title = sis_get_title();
	__get_titles_callback(url, title);
}

var __log_cnt = 0;

function log(msg) {
	var div = document.createElement("div");
	div.innerText = msg;
	log_div.appendChild(div);
	check_log_limit();
}

function error(msg) {
	var div = document.createElement("div");
	div.style.color = 'red';
	div.innerText = msg;
	log_div.appendChild(div);
	if (body != null) 
		log(body);	
	check_log_limit();
}

function check_log_limit() {
	if (__log_cnt > 1000) {
		clear_log();
		__log_cnt = 0;
	} 
	__log_cnt++;
}
	
function clear_log() {
	log_div.innerHTML = '';
}

